﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LoanPrequalificationSystem.Model
{
    public class Address : IAddress
    {
        private string _street;
        private string _postCode;
        private string _town;
        private string _city;


        public Address()
        { }

        public Address(string street, string town, string postcode, string city)
        {
            this._city = city;
            this._postCode = postcode;
            this._street = street;
            this._town = town;
            if (GetBrokenRules().Count > 0) 
            {
                throw new Exception(String.Format("The Address you are trying to create is invalid.{0}",GetBrokenRules()));
            }
        }

        public string City
        {
            get { return _city; }
        }

        public string PostCode
        {
            get { return _postCode; }
        }

        public string Street
        {
            get { return _street; }
        }

        public string Town
        {
            get { return _town; }
        }

        public List<BrokenBusinessRule> GetBrokenRules()
        {
            List<BrokenBusinessRule> brokenRules = new List<BrokenBusinessRule>();

            if (string.IsNullOrEmpty(Street)) 
            {
                brokenRules.Add(new BrokenBusinessRule("Street", "A Street must be defined for an address"));
            }
            if (string.IsNullOrEmpty(Town)) 
            {
                brokenRules.Add(new BrokenBusinessRule("Town", "A Town must be defined for an address"));
            }
            if (string.IsNullOrEmpty(PostCode))
            {
                brokenRules.Add(new BrokenBusinessRule("PostCode", "A PostCode must be defined for an address"));
            }
            if (string.IsNullOrEmpty(City))
            {
                brokenRules.Add(new BrokenBusinessRule("City", "A City must be defined for an address"));
            }
            return brokenRules;
        }
        private string GetBrokenBusinessRuleToString() 
        {
            StringBuilder sbBrokenRules = new StringBuilder();
            foreach (BrokenBusinessRule rule in GetBrokenRules())
            {
                sbBrokenRules.Append(rule.Rule);
            }
            return sbBrokenRules.ToString();
        }
    }
}
